package dev.ece.core.cells;

import java.util.Vector;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import dev.ece.core.clearup.ClearUpGLRubbish;
import dev.ece.util.GLUtils;
import dev.ece.util.tools.Camera;
import dev.ece.util.tools.transformations.Transformation;

/**
 * 图元接口
 * @author 飘渺青衣
 * <b>E-mail：</b>3375758@qq.com
 */
public interface Cell extends CellEvent, ClearUpGLRubbish {

	public static final Logger LOG = LogManager.getLogger(Cell.class);
	
	/**
	 * 获取模型转换
	 * @return
	 */
	public Vector<Transformation> getTransformations();
	
	/**
	 * 计算矩阵
	 */
	default void calculate() {
		GLUtils.transform(getTransformations());
	}
	
	/**
	 * 由于vao无法多线程共享，所以这里一般初始化vao
	 */
	default void initialize() {
		if(!this.isInitialized()) {
			try {
				this.onInitialize();
			} catch(Exception e) {
				LOG.error("初始化异常", e);
			}
			this.setInitialized(true);
		}
	}
	
	/**
	 * 逻辑运算
	 */
	default void logic(Camera camera) {
		try {
			this.onLogic(camera);
		} catch(Exception e) {
			LOG.error("逻辑运算异常", e);
		}
	}
	
	/**
	 * 渲染
	 */
	default void render(Camera camera) {
		GLUtils.glPushMatrix();
		try {
			this.calculate();
			this.onRender(camera);
		} catch(Exception e) {
			LOG.error("渲染普通物体异常", e);
		}
		GLUtils.glPopMatrix();
	}
	
	/**
	 * 摧毁
	 */
	public void destroy();
	
	/**
	 * 是否初始化完成
	 * @return
	 */
	public boolean isInitialized();
	
	/**
	 * 设置初始化完成
	 * @param initialized
	 */
	public void setInitialized(boolean initialized);
	
	/**
	 * 是否可见
	 * @return
	 */
	public boolean isVisible();

	/**
	 * 是否逻辑运算
	 * @return
	 */
	public boolean isLogic();

}
